﻿namespace zijian666.DI;

/// <summary>
/// 自动装配特性
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class AutowiredAttribute : Attribute
{
    /// <summary>
    /// 待装配服务类型,默认自动推断
    /// </summary>
    public Type? ServiceType { get; }

    /// <summary>
    /// 服务键
    /// </summary>
    public object? Key { get; set; }

    /// <summary>
    /// 是否使用 <see cref="System.ComponentModel.DataAnnotations.Validator"/> 对服务进行验证 
    /// </summary>

    public bool Validate { get; set; } = false;

    /// <summary>
    /// 自定义装配处理器
    /// </summary>
    public Type AutowiredHandler { get; set; }

    /// <summary>
    /// 初始化 <see cref="AutowiredAttribute"/> 类的新实例。
    /// </summary>
    public AutowiredAttribute() { }

    /// <summary>
    /// 使用指定的服务类型初始化 <see cref="AutowiredAttribute"/> 类的新实例。
    /// </summary>
    /// <param name="serviceType">待装配服务类型</param>
    public AutowiredAttribute(Type? serviceType)
        => ServiceType = serviceType;

    /// <summary>
    /// 使用指定的服务键初始化 <see cref="AutowiredAttribute"/> 类的新实例。
    /// </summary>
    /// <param name="key">服务键</param>
    public AutowiredAttribute(object key)
        => Key = key;

    /// <summary>
    /// 使用指定的服务键和服务类型初始化 <see cref="AutowiredAttribute"/> 类的新实例。
    /// </summary>
    /// <param name="key">服务键</param>
    /// <param name="serviceType">待装配服务类型</param>
    public AutowiredAttribute(object key, Type serviceType)
        : this(serviceType)
        => Key = key;
}
